//package com.smartcar.gateway.filters;
//
//import com.smartcar.gateway.utils.AesUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.reactivestreams.Publisher;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.core.io.buffer.DataBufferFactory;
//import org.springframework.core.io.buffer.DataBufferUtils;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Flux;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//
///**
// * @author: Zek
// * @date: 2020/8/13 on 4:36 下午
// * @description:
// */
//@Slf4j
//@Configuration
//public class ResponseEncryptFilter implements GlobalFilter, Ordered {
//
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        // 数据响应拦截（加密）
//        ServerHttpResponseDecorator responseDecorator = processResponse(exchange.getResponse(), exchange.getResponse().bufferFactory());
//        return chain.filter(exchange.mutate().response(responseDecorator).build());
//    }
//
//    /**
//     * 数据响应拦截（加密处理）
//     * @param serverHttpResponse    响应对象
//     * @param dataBufferFactory     数据Buffer工厂
//     * @return  返回拦截器
//     */
//    private ServerHttpResponseDecorator processResponse(ServerHttpResponse serverHttpResponse, DataBufferFactory dataBufferFactory){
//        return new ServerHttpResponseDecorator(serverHttpResponse) {
//            @Override
//            public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
//                if(body instanceof Flux){
//                    Flux<? extends DataBuffer> flux = (Flux<? extends DataBuffer>) body;
//                    return super.writeWith(flux.buffer().map(dataBuffers -> {
//                        StringBuilder dataBuilder = new StringBuilder();
//                        // Gateway网关数据太长会进行分段传输
//                        dataBuffers.forEach(dataBuffer -> {
//                            // 数据读取
//                            byte[] content = new byte[dataBuffer.readableByteCount()];
//                            dataBuffer.read(content);
//                            // 释放内存
//                            DataBufferUtils.release(dataBuffer);
//                            // 数据拼接
//                            dataBuilder.append(new String(content, StandardCharsets.UTF_8));
//                        });
//                        // 对数据进行加密
//                        String result = null;
//                        try {
//                            result = AesUtil.encrypt(dataBuilder.toString(),AesUtil.AES_KEY,AesUtil.IV);
//                        } catch (Exception exception) {
//                            exception.printStackTrace();
//                        }
//                        // 写入数据
//                        return dataBufferFactory.wrap(result.getBytes(StandardCharsets.UTF_8));
//                    }));
//                }
//                return super.writeWith(body);
//            }
//        };
//    }
//    /** 顺序:数字越小，越先执行 */
//    @Override
//    public int getOrder() {
//        return -1;
//    }
//
//
//}
